package com.joker.airoleplayplatformback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joker.airoleplayplatformback.domain.po.UserPO;
import com.joker.airoleplayplatformback.mapper.UserMapper;
import com.joker.airoleplayplatformback.service.UserService;
import com.joker.airoleplayplatformback.utils.JwtUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * 用户服务实现类
 *
 * @author feixiang.li
 * @since 2025-09-27
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Resource
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public UserPO register(String username, String password, String nickname, String email) {
        // 检查用户名是否已存在
        if (existsByUsername(username)) {
            throw new RuntimeException("用户名已存在");
        }

        // 检查邮箱是否已存在
        if (email != null && existsByEmail(email)) {
            throw new RuntimeException("邮箱已存在");
        }

        // 创建用户
        UserPO user = new UserPO();
        user.setUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        user.setNickname(nickname != null ? nickname : username);
        user.setEmail(email);
        user.setStatus(0);

        userMapper.insert(user);
        log.info("用户注册成功: {}", username);

        return user;
    }

    @Override
    public String login(String username, String password) {
        UserPO user = findByUsername(username);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        if (user.getStatus() == 1) {
            throw new RuntimeException("用户已被禁用");
        }

        if (!passwordEncoder.matches(password, user.getPassword())) {
            throw new RuntimeException("密码错误");
        }

        // 生成JWT Token
        String token = jwtUtils.generateToken(user.getId(), user.getUsername(), user.getNickname());
        log.info("用户登录成功: {}", username);

        return token;
    }

    @Override
    public UserPO findByUsername(String username) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getUsername, username);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public UserPO findById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public UserPO updateUser(UserPO user) {
        userMapper.updateById(user);
        return user;
    }

    @Override
    public boolean existsByUsername(String username) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getUsername, username);
        return userMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public boolean existsByEmail(String email) {
        if (email == null || email.trim().isEmpty()) {
            return false;
        }
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getEmail, email);
        return userMapper.selectCount(queryWrapper) > 0;
    }
}
